package com.qf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qf.dao.HotelMapper;
import com.qf.dao.HotelOrderDetailMapper;
import com.qf.dao.HotelOrderMapper;
import com.qf.dao.repository.CityRepositroy;
import com.qf.dao.repository.HotelRoomRepository;
import com.qf.pojo.po.*;
import com.qf.pojo.req.XcHotelCityAreaReq;
import com.qf.pojo.req.XcHotelOrderReq;
import com.qf.pojo.resp.BaseResp;
import com.qf.pojo.resp.XcHotelOrderResp;
import com.qf.pojo.resp.XcHotelResp;
import com.qf.service.HotelService;
import com.qf.utils.GenerateOrderNum;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class HotelServiceImpl implements HotelService {

    @Autowired
    HotelMapper hotelMapper;
    @Autowired
    HotelRoomRepository hotelRoomRepository;
    @Autowired
    CityRepositroy cityRepositroy;
    @Autowired
    HotelOrderMapper orderMapper;
    @Autowired
    HotelOrderDetailMapper orderDetailMapper;
    @Autowired
    RedisTemplate redisTemplate;


    //酒店首页,推荐酒店查询
    @Override
    public BaseResp recommendFind() {
        List<XcHotelResp> xcHotelResps = hotelMapper.recommend ();
        return new BaseResp (0,"查询成功",null,xcHotelResps);
    }

    //查询城市信息
    @Override
    public BaseResp cityFind() {
        List<XcCity> all = cityRepositroy.findAll ();
        return new BaseResp (0,"查询成功",null,all);
    }

    @Override
    public BaseResp hotelOrderAll(HttpServletRequest request) {
        //查询用户是佛登录
        String token = request.getHeader ("token");
        Object o = redisTemplate.opsForValue ().get (token);
        XcUser xcUser = JSONObject.parseObject (JSONObject.toJSONString (o), XcUser.class);
        if (xcUser==null){
            return new BaseResp (0,"登录过期,请重新登录",null,null);
        }
        List<XcHotelOrderResp> xcOrderResps = orderDetailMapper.xcOrderlistAll(xcUser.getId ());
        return new BaseResp (0,"订单查询成功",null,xcOrderResps);
    }

    //根据城市名查找对应的酒店
    @Override
    public BaseResp cityHotelName(XcHotelCityAreaReq xcHotelCityAreaReq) {
        Optional<XcCity> byId = cityRepositroy.findById (xcHotelCityAreaReq.getCityId ());
        List<XcHotelResp> xcHotelResps = hotelMapper.cityHotelName (byId.get ().getCityName ());
        return new BaseResp (0,"查询成功",null,xcHotelResps);
    }

    //点击酒店查询显示酒店详情
    @Override
    public BaseResp hotelDetails(Integer hotelId) {
        XcHotelResp xcHotelResp = hotelMapper.hotelDetails (hotelId);
        return new BaseResp (0,"查询成功",null,xcHotelResp);
    }


    //根据酒店不同房间类型进行预订并存入订单表中,生成订单
    @Override
    public BaseResp hotelReserve(XcHotelOrderReq xcHotelOrderResp, HttpServletRequest request) {
        //方法进入后先判断该房间数量
        Optional<XcHotelRoom> byId = hotelRoomRepository.findById (xcHotelOrderResp.getRoomId ());
        Integer Num = byId.get ().getRoomNum ();
        if (Num<(1)){
            return new BaseResp (1,"房间数量不足,请重新选定",null,Num);
        }

        //判断redis中是否存在token值
        String token = request.getHeader ("token");
        Object o = redisTemplate.opsForValue ().get (token);
        //判断用户是否登录
        XcUser xcUser = JSONObject.parseObject (JSONObject.toJSONString (o), XcUser.class);
        if (xcUser==null){
            return new BaseResp (0,"登录过期,请重新登录",null,null);
        }

        synchronized (this){
            //生成订单号
            String generate = GenerateOrderNum.generate("");
            //生成订单
            XcOrder xcOrder=new XcOrder();
            xcOrder.setOrderId (generate);
            xcOrder.setOrderPay (xcHotelOrderResp.getMoney ());
            xcOrder.setOrderStatus (1);
            xcOrder.setOrderType (4);
            xcOrder.setOrderUserId (xcUser.getId ());
            xcOrder.setCreateTime (new Date ());
            //调用dao层方法储存订单
            orderMapper.addOrder (xcOrder);

            //生成子订单
            String generate2 = GenerateOrderNum.generate("");
            XcOrderDetail xcOrderDetail=new XcOrderDetail();
            xcOrderDetail.setOrderDetailId (generate2);
            xcOrderDetail.setCreateTime (new Date ());
            xcOrderDetail.setOrderId (xcOrder.getOrderId ());
            xcOrderDetail.setHotelRomeId (xcHotelOrderResp.getRoomId ());
            //调用dao层方法
            orderDetailMapper.addOrderDetail(xcOrderDetail);

            //订单生成后,数据库对应的房间数量减少
            int roomNum=Num-1;
            int i =hotelMapper.updateRoomNum(roomNum,xcHotelOrderResp.getRoomId ());
            redisTemplate.opsForValue ().set ("roomNum",roomNum);
            redisTemplate.opsForValue ().set ("roomId",xcHotelOrderResp.getRoomId ());
        }
        return new BaseResp (0,"订单生成成功",null,null);
    }


    //从数据库查询订单并展示
    @Override
    public BaseResp hotelOrder(HttpServletRequest request) {
        //查询用户是佛登录
        String token = request.getHeader ("token");
        Object o = redisTemplate.opsForValue ().get (token);
        XcUser xcUser = JSONObject.parseObject (JSONObject.toJSONString (o), XcUser.class);
        if (xcUser==null){
            return new BaseResp (0,"登录过期,请重新登录",null,null);
        }
        List<XcHotelOrderResp> xcOrderResps = orderDetailMapper.xcOrderlist (xcUser.getId ());
        return new BaseResp (0,"订单查询成功",null,xcOrderResps);
    }

    //取消订单
    @Override
    public BaseResp checkOut(String orderId) {
        int a = orderDetailMapper.deleteOrderDetail (orderId);
        int b = orderMapper.deleteOrder (orderId);
        if (a>0 && b>0){
            //取消订单后,房间数量回加
            Integer Num = (Integer) redisTemplate.opsForValue ().get ("roomNum");
            Integer roomId = (Integer)redisTemplate.opsForValue ().get ("roomId");
            int roomNum=Num+1;
            hotelMapper.addRoomNum(roomNum,roomId);
            return new BaseResp (0,"订单删除成功",null,null);
        }
        return new BaseResp (1,"订单删除失败,请重新加载",null,null);
    }



}
